<template>
  <div>
    <lb-title></lb-title>
    <div class="page-main">
      <div class="page-top-operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="jumpToEditPage('add')"
          >新增版权</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="100px"> </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column label="版权图片">
          <template slot-scope="scope">
            <lb-image :src="scope.row.image"></lb-image>
          </template>
        </el-table-column>
        <el-table-column prop="text" label="版权文字"> </el-table-column>
        <el-table-column prop="phone" label="联系号码"> </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="jumpToEditPage('edit', scope.row.id)"
                >{{ $t('action.edit') }}</el-button
              >
              <el-button
                type="danger"
                size="mini"
                plain
                @click="deleteCopyrightInfo(scope.row.id)"
                >{{ $t('action.delete') }}</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="pageForm.page"
        :pageSize="pageForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'Copyright',
  data () {
    return {
      loading: false,
      pageForm: {
        page: 1,
        page_count: 10
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      this.$api.getCopyrightList(this.pageForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    jumpToEditPage (type, id = '') {
      this.$router.push(`/copyright/edit${type === 'edit' ? '?id=' + id : ''}`)
    },
    deleteCopyrightInfo (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.delCopyrightInfo({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.getTableDataList()
          }
        })
      })
    },
    handleSizeChange (val) {
      this.pageForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.pageForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
